package com.scuec.tourismwebsite.mapper;

import com.scuec.tourismwebsite.entity.Admin;
import com.scuec.tourismwebsite.entity.User;
import com.scuec.tourismwebsite.vo.UserPost;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {
    //查找所有
    @Select("select * from user ORDER BY followers_count DESC")
    List<User> findUserAll();

    //根据id删除
    @Delete("delete from user where user_id=#{user_id}")
    int deleteUserById(int user_id);

    @Select("SELECT * FROM user WHERE nickname LIKE CONCAT('%', #{nickname}, '%')")
    List<User> findUserByName(String nickname);

    @Insert("INSERT INTO user (nickname, password, phone_number, avatar, profile, followers_count, following_count, likes_count, favorites_count, posts_count, create_date, ip_address) " +
            "VALUES (#{nickname}, #{password}, #{phone_number}, #{avatar}, #{profile}, #{followers_count}, #{following_count}, #{likes_count}, #{favorites_count}, #{posts_count}, NOW(), #{ip_address})")
    int addUser(User user);

    @Select("SELECT * FROM user WHERE phone_number = #{phone_number}")
    User findUserByPhone(String phone_number);

    @Update("UPDATE user SET nickname=#{nickname}, phone_number=#{phone_number}, avatar=#{avatar}, profile=#{profile} WHERE user_id=#{user_id}")
    int updateUser(User user);
    @Update("UPDATE user SET password=#{newPassword} WHERE user_id=#{user_id}")
    int updatePassword(@Param("user_id") int user_id, @Param("newPassword") String newPassword);

    @Select("SELECT * FROM post WHERE user_id = #{user_id}")
    List<UserPost> getUserPostsByUserId(int user_id);

    @Select("SELECT * FROM user WHERE user_id = #{user_id}")
    User getUserById(int user_id);

    @Select("SELECT * FROM user WHERE phone_number = #{phone}")
    User findByPhoneNumber(@Param("phone") String phone);

    @Select("SELECT COUNT(*) FROM user")
    int countUsers();

    @Update("UPDATE user SET following_count=#{following_count} WHERE user_id=#{user_id}")
    int updateFollowingCount(@Param("following_count")int following_count,@Param("user_id") int user_id);

    @Update("UPDATE user SET followers_count=#{followers_count} WHERE user_id=#{user_id}")
    int updateFollowersCount(@Param("followers_count")int followers_count,@Param("user_id")int user_id);
}
